import { router } from 'umi';
import * as shopApi from '@/services/order';
import { message } from 'antd';
import { closeBroken } from "@/services/order";

export default {
  namespace: 'order',
  state: {
    allList: [], //全部列表
    allTotal: 1,
    allListCount: [], //全部订单类型数据
    allListExt: [], //全部列表带详情
    allTotalExt: 1,
    closeList: [], //关闭列表
    closeTotal: 1,
    conditionList: [], //逾期列表列表
    conditionTotal: 1,
    BuyOutList: [], //买断订单列表
    BuyOutTotal: 1,
    ReletList: [], //查询续租订单
    ReletTotal: 1,
    AuditList: [], //电审
    AuditTotal: 1,
    PlateformList: [], //获取上架渠道集合
    userOrderInfoDto: {}, //订单信息
    shopInfoDto: {}, //商家信息
    orderAddressDto: {}, //收货人信息
    receiptExpressInfo: {}, //发货物流信息
    giveBackExpressInfo: {}, //归还物流信息
    productInfo: [],
    opeRemarkDtoPage: {}, //平台备注
    // OrderRemarkDataTotal: 1,
    businessRemarkDtoPage: {}, //商家备注
    // businessRemarkDtoTotal: 1,
    userOrderCashesDto: [], //账单信息
    orderByStagesDtoList: [], //分期信息
    orderBuyOutDto: [], //买断信息
    wlList: [], //物流信息
    originalOrderDto: [], //原定单详情
    userOrderBuyOutDto: [], //买断信息
    userOrderPurchaseList: [],
    userOrderPurchaseTotal: 1,
    sourceList: [] // 顾客来源渠道
  },
  effects: {
    * queryOpeOrderByConditionList({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeOrderByCondition, payload);

      yield put({
        type: 'allListData',
        payload: response,
      });
    },
    * queryOrderGroupCountByCondition({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOrderGroupCountByCondition, payload);

      yield put({
        type: 'allListCount',
        payload: response,
      });
    },
    // 查询所有订单带详情
    * queryOrderByConditionExt({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOrderByConditionExt, payload);

      yield put({
        type: 'allListDataExt',
        payload: response,
      });
    },
    * userOrderPurchase({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.userOrderPurchase, payload);

      yield put({
        type: 'userOrderPurchaseData',
        payload: response,
      });
    },
    * queryPendingOrderClosureList({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryPendingOrderClosureList, payload);

      yield put({
        type: 'closeListData',
        payload: response,
      });
    },
    * closeUserOrderAndRefundPrice({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.closeUserOrderAndRefundPrice, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * queryOrderStagesDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOrderStagesDetail, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * queryOpeOverDueOrdersByCondition({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeOverDueOrdersByCondition, payload);

      yield put({
        type: 'conditionListData',
        payload: response,
      });
    },
    * queryOpeGoingOverDueOrdersByCondition({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeGoingOverDueOrdersByCondition, payload);

      yield put({
        type: 'conditionListData',
        payload: response,
      });
    },
    * queryWaitingGiveBackOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryWaitingGiveBackOrder, payload);

      yield put({
        type: 'waitingGiveBackListData',
        payload: response,
      });
    },
    * queryOpeBuyOutOrdersByCondition({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeBuyOutOrdersByCondition, payload);

      yield put({
        type: 'BuyOutListData',
        payload: response,
      });
    },
    * queryTelephoneAuditOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryTelephoneAuditOrder, payload);

      yield put({
        type: 'AuditListData',
        payload: response,
      });
    },
    * queryReletOrderByCondition({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryReletOrderByCondition, payload);

      yield put({
        type: 'ReletListData',
        payload: response,
      });
    },
    * PlateformChannel({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.PlateformChannel, payload);
      yield put({
        type: 'PlateformData',
        payload: response,
      });
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * SourceList({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.SourceList, payload);
      yield put({
        type: 'SourceData',
        payload: response,
      });
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * orderRemark({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.orderRemark, payload);

      message.warning('备注成功～');
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * telephoneAuditOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.telephoneAuditOrder, payload);
      message.warning('备注成功～');
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },

    * queryOpeUserOrderDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeUserOrderDetail, payload);

      yield put({
        type: 'OpeUserData',
        payload: response,
      });
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * queryOpeBuyOutOrderDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeBuyOutOrderDetail, payload);
      yield put({
        type: 'OpeUserData',
        payload: response,
      });
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },

    //续租
    * queryUserReletOrderDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryUserReletOrderDetail, payload);
      yield put({
        type: 'OpeUserData',
        payload: response,
      });
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * userOrdersPurchase({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.userOrdersPurchase, payload);

      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * purchaseOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.purchaseOrder, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * queryOrderRemark({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOrderRemark, payload);
      // console.log(payload.source);
      yield put({
        type: 'OrderRemarkData',
        payload: {
          response,
          source: payload.source,
        },
      });
    },
    * queryExpressInfo({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryExpressInfo, payload);
      yield put({
        type: 'queryInfoList',
        payload: response,
      });
    },
    * expressInfo({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.expressInfo, payload);
      yield put({
        type: 'queryInfoList',
        payload: response,
      });
    },
    * contractReport({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.contractReport, payload);
      window.open(response.data);
    },
    * selectDistrict({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.selectDistrict, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * opeOrderAddressModify({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.opeOrderAddressModify, payload);
      if (callback && typeof callback === 'function') {
        callback(response);
      }
    },
    * exportOpeAllUserOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportOpeAllUserOrders, payload);
      location.href = response.data;
      message.success('导出成功～');
    },

    * purchaseOrderExport({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.purchaseOrderExport, payload);
      location.href = response.data;
      message.success('导出成功～');
    },

    * exportOpeBuyOutUserOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportOpeBuyOutUserOrders, payload);
      location.href = response.data;
      message.success('导出成功～');
    },
    * exportOpeOverDueUserOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportOpeOverDueUserOrders, payload);
      location.href = response.data;
      message.success('导出成功～');
    },
    * exportWaitingGiveBack({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportWaitingGiveBack, payload);
      location.href = response.data;
      message.success('导出成功～');
    },
    * exportReletOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportReletOrders, payload);
      location.href = response.data;
      message.success('导出成功～');
    },
    * exportTelephoneAuditOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportTelephoneAuditOrders, payload);
      location.href = response.data;
      message.success('导出成功～');
    },
    * exportCloseRefundOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportCloseRefundOrders, payload);
      location.href = response.data;
      message.success('导出成功～');
    },
    // 获取信用详情
    * getCredit({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.getSiriusReportRecord, payload);
      if (callback) callback(res);
    },
    * getCreditV3({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.getSiriusReportRecordV3, payload);
      if (callback) callback(res);
    },
    * getCreditV4({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.getSiriusReportRecordV4, payload);
      if (callback) callback(res);
    },
    // 获取蚂蚁链信息
    * getChainInfoByOrderId({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.getChainInfoByOrderId, payload);
      if (callback) callback(res);
    },
    // 查询蚁盾分
    * getUserRisk({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.getUserRisk, payload);
      if (callback) callback(res);
    },
    // 退租
    * quitLease({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.quitLease, payload);
      if (callback) callback(res);
    },
    // 完结订单
    * comleteOrder({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.comleteOrder, payload);
      if (callback) callback(res);
    },
    // 关闭分期订单-线下履约
    * closeStageOrder({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.closeStageOrder, payload);
      if (callback) callback(res);
    },
    // 碎屏险-线下履约
    * closeBroken({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.closeBroken, payload);
      if (callback) callback(res);
    },
    // 逾期费-线下履约
    * closeOverdue({ payload, callback }, { put, call }) {
      let res = yield call(shopApi.closeOverdue, payload);
      if (callback) callback(res);
    },
  },
  reducers: {
    allListData(state, { payload }) {
      return {
        ...state,
        allList: payload.data.records,
        allTotal: payload.data.total,
      };
    },
    // 带详情数据
    allListDataExt(state, { payload }) {
      return {
        ...state,
        allListExt: payload.data.records,
        allTotalExt: payload.data.total,
      };
    },
    // 带详情数据
    allListCount(state, { payload }) {
      return {
        ...state,
        allListCount: payload.data
      };
    },
    userOrderPurchaseData(state, { payload }) {
      return {
        ...state,
        userOrderPurchaseList: payload.data.records,
        userOrderPurchaseTotal: payload.data.total,
      };
    },
    closeListData(state, { payload }) {
      return {
        ...state,
        closeList: payload.data.records,
        closeTotal: payload.data.total,
      };
    },
    conditionListData(state, { payload }) {
      return {
        ...state,
        conditionList: payload.data.records,
        conditionTotal: payload.data.total,
      };
    },
    waitingGiveBackListData(state, { payload }) {
      return {
        ...state,
        waitingGiveBackList: payload.data.records,
        waitingGiveBackTotal: payload.data.total,
      };
    },
    AuditListData(state, { payload }) {
      return {
        ...state,
        AuditList: payload.data.records,
        AuditTotal: payload.data.total,
      };
    },
    BuyOutListData(state, { payload }) {
      return {
        ...state,
        BuyOutList: payload.data.records,
        BuyOutTotal: payload.data.total,
      };
    },
    ReletListData(state, { payload }) {
      return {
        ...state,
        ReletList: payload.data.records,
        ReletTotal: payload.data.total,
      };
    },
    PlateformData(state, { payload }) {
      return {
        ...state,
        PlateformList: payload.data,
      };
    },
    SourceData(state, { payload }) {
      return {
        ...state,
        sourceList: payload.data,
      };
    },
    OpeUserData(state, { payload }) {
      // console.log('payload:', payload);
      return {
        ...state,
        ...payload.data,
        productInfo: [payload.data.productInfo],
        // OrderRemarkDataList: payload.data.opeRemarkDtoPage.records,
        // OrderRemarkDataTotal: payload.data.opeRemarkDtoPage.total,
        // businessRemarkDtoList: payload.data.businessRemarkDtoPage.records,
        // businessRemarkDtoTotal: payload.data.businessRemarkDtoPage.total,
        userOrderCashesDto: [payload.data.userOrderCashesDto],
        orderByStagesDtoList: payload.data.orderByStagesDtoList,
        orderBuyOutDto: [payload.data.orderBuyOutDto],
        originalOrderDto: [payload.data.originalOrderDto],
        userOrderBuyOutDto: [payload.data.userOrderBuyOutDto],
      };
    },
    OrderRemarkData(state, { payload }) {
      const { source, response } = payload;
      if (source === '01') {
        return {
          ...state,
          opeRemarkDtoPage: response.data,
        };
      }
      return {
        ...state,
        businessRemarkDtoPage: response.data,
      };
    },
    queryInfoList(state, { payload }) {
      return {
        ...state,
        wlList: payload.data.result.list,
      };
    },
  },
};
